<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

function rendfield($item, $field, $event = false, $view = false, $is_filter = false) {
	if(!$field) return;
	$remove_field_name = array ('views', 'id', 'ordering' );
	if (in_array ( $field->field_name, $remove_field_name ))
		return false;
	global $option, $mainframe, $jbconfig;

	if ($field->field_type != 'CHECKBOXLIST' && $field->field_type != 'MSELECT' && isset ( $item->{$field->field_name} )) {
		if (is_string($item->{$field->field_name}))
		$item->{$field->field_name} = htmlspecialchars_decode($item->{$field->field_name});
	}
	$r = "";
		
	JHTML::_ ( 'behavior.tooltip' );
	JHTML::_ ( 'behavior.mootools' );
	JHTML::_ ( 'behavior.calendar' );
	if (($field->eventmouse != null) && ($field->eventmouse != '')) {
		if ($field->functionjs != null)
			$mouse_event = $field->eventmouse . "='" . $field->functionjs . "'";
		else
			$mouse_event = '';
	} else
		$mouse_event = '';
	if (($field->field_class != null) && ($field->field_class != ''))
		$class = "class='$field->field_class'";
	else
		$class = "class='inputbox'";
	$javascript = '';
	if ($event)
		$javascript = "onchange='form.submit()'";

	if (isset ( $item->{$field->field_name} )){
		$value = ($item->{$field->field_name} != null) ? $item->{$field->field_name} : $field->field_init;
	}
	else
		$value = $field->field_init;
	
	include_once ("components/$option/models/jacodes.php");
	$model_codes = new JAJobBoardModelJACodes ( );
			
	$comment = $example = '';
	if ($field->field_comment != '')
		$comment = ' <span class="editlinktip hasTip" title="<b>' . JText::_ ( $field->label ) . '</b>:<br />' . JText::_ ( $field->field_comment ) . '"><img border="0" alt="Tooltip" src="' . $mainframe->getSiteUrl () . 'includes/js/ThemeOffice/tooltip.png"/></span>';
	if ($field->field_example != '')
		$example = JText::_ ( $field->field_example );
		
	if ($field->is_referred) {
		$r = getExternalCombo ( $field, $value, $view, $javascript, 1, $class, $mouse_event,$is_filter );
	} else {
		$readonly = $field->is_readonly ? 'readonly' : '';
		switch (trim ( $field->field_type )) {
			case 'BLANK' :
				$r = '';
				break;
			case "BOOLEAN" :
				$r = JHTML::_ ( 'select.booleanlist', $field->field_name, $class . ' ' . $mouse_event, $value );
				break;
			case "DATE" :
				$r = "<input name='" . $field->field_name . "' id='" . $field->field_name . "' value='" . $value . "' $javascript $class $mouse_event/>";
				break;
			case "DATE_CAL" :
				
				$dateFormat = $jbconfig['general']->get('format_date');
				if($dateFormat){
					$dateFormat = explode(" ", $dateFormat);
					if( isset ($dateFormat[1]) )
						$dateFormat = $dateFormat[0]." ".str_replace(array("i", "s"), array("M", "S"),$dateFormat[1]);
					else $dateFormat = $dateFormat[0];
				}else $dateFormat = "%Y-%m-%d %H:%M:%S";
								
				if($value)
					$value = date( str_replace(array("%", "M", "S"), array("", "i", "s"), $dateFormat), strtotime($value));
					
				$r = JHTML::_ ( 'calendar', $value, $field->field_name, $field->field_name, $dateFormat, array ('class' => isset ( $field->field_class ) ? $field->field_class : "inputbox", 'size' => '25', 'maxlength' => '19', $field->eventmouse => $field->functionjs ) );
				break;
			case 'FILE' :
				if ((JRequest::getVar ( 'view' ) == 'jaapplications') && ($field->field_name == 'attachment') && ($item->opt_attachment))
					$input_disabled = 'disabled="disabled"';
				else
					$input_disabled = '';
				$current_view = JRequest::getVar ( 'view' );
				if ($value) {
					$file_name = explode ( '/', $item->{$field->field_name} );
					$r = '<span id="download_' . $field->field_name . '"><a target="_blank" href ="' . JRoute::_ ( 'index.php?option=com_jajobboard&view=' . $current_view . '&task=download&field_name='.$field->field_name.'&cid[]=' . $item->id ) . '">' . JText::_ ( substr ( $file_name [count ( $file_name ) - 1], 14 ) ) . '</a></span>&nbsp&nbsp';
					$r .= '<span id="remove_' . $field->field_name . '">(<a href ="javascript:void(0)" onclick="remove_attach(\'' . $field->field_name . '\',\'' . $item->id . '\')">' . JText::_ ( 'Remove' ) . '</a>)</span><br />';
					$r .= '<input type="hidden" name="text_' . $field->field_name . '" value="' . $value . '" id="text_' . $field->field_name . '" />';
				}
				$r .= "<input $input_disabled type='FILE' name='" . $field->field_name . "' id='" . $field->field_name . "' value='' $javascript $class $mouse_event/> ";
				break;
			case 'IMAGE' :
				if ($value) {
					$src = JURI::root () . $value;
					$temp_path = str_replace ( '\\', DS, JPATH_SITE . DS . $value );
					$temp_path = str_replace ( '/', DS, $temp_path );
					if (file_exists ( $temp_path )) {
						$imgInfo = getimagesize ( $temp_path );
						$image_width = $imgInfo [0];
						$image_height = $imgInfo [1];
						$r = '<span id="download_' . $field->field_name . '">' . "<img src='" . $src . "' alt='' width='" . $image_width . "' height='" . $image_height . "'/>" . '</span><br />';
						$r .= '<span id="remove_' . $field->field_name . '">(<a href ="javascript:void(0)" onclick="remove_image(\'' . $field->field_name . '\',\'' . $item->id . '\')">' . JText::_ ( 'Remove' ) . '</a>)</span><br />';
					} else
						$r = "<img src='" . JURI::base () . "/components/com_jajobboard/images/no-image.gif' alt='Default'/>";
				
				} else {
					
					$r = "<img src='" . JURI::base () . "/components/com_jajobboard/images/no-image.gif' alt='Default'/>";
				}
				$r .= "<br/><input type='FILE' name='" . $field->field_name . "' id='" . $field->field_name . "' value='' $class  $mouse_event/>";
				$r .= '<input type="hidden" name="text_' . $field->field_name . '" value="' . $value . '" id="text_' . $field->field_name . '" />';
				break;
			case 'URL' :
			case 'TEXTFIELD' :
			case 'NUMERIC' :
			case 'INTEGER' :
				$field->field_height = $field->field_height ? $field->field_height : 18;
				$field->field_width = $field->field_width ? $field->field_width : 150;
				$r = "<input type='text' name='" . $field->field_name . "' id='" . $field->field_name . "' $readonly value='" . $value . "' style=\"width:" . $field->field_width . "px;height:" . $field->field_height . "px;\"  $class  $mouse_event/> ";
				break;
			case 'PASS' :
				$field->field_height = $field->field_height ? $field->field_height : 18;
				$field->field_width = $field->field_width ? $field->field_width : 150;
				$r = "<input $mouse_event $class type='password' name='" . $field->field_name . "' id='" . $field->field_name . "' $readonly value='' style=\"width:" . $field->field_width . "px;height:" . $field->field_height . "px;\" /> ";
				break;
			
			case "EDITOR" :
				$editor = &JFactory::getEditor ();
				
				$field->field_height = $field->field_height ? $field->field_height : 200;
				$field->field_width = $field->field_width ? $field->field_width : 350;
				
				// parameters : areaname, content, width, height, cols, rows
				//$post    = JRequest::get('request', JREQUEST_ALLOWHTML);
				//if( isset($post[$field->field_name]) )
				    //$value = $post[$field->field_name];
				
				$r = $editor->display ( $field->field_name, $value, '100%', $field->field_height, '10', '5', FALSE );
				break;
			case 'MSELECT' :
				if (! is_array ( $value ))
					$value = explode ( ',', $value );
				JRequest::setVar ( 'field_id', $field->table_name . '_' . $field->field_name );
				$arr_option = $model_codes->getItems ();
				for ($i=0;$i<count($arr_option);$i++)
					$arr_option[$i]->text = JText::_($arr_option[$i]->text);
				if ($field->field_name != 'apply_type') {
					if (!is_array($value))
						$value = explode ( ',', $value );
					
					$select_options = array ();
					$select_options [0]->value = '';
					if ($is_filter)
						$select_options [0]->text = JText::_ ( 'Please select' ).' '.$field->label . '...';
					else 
						$select_options [0]->text = JText::_ ( 'Please select' ). '...';
					for($i = 0; $i < count ( $arr_option ); $i ++)
						$select_options [] = $arr_option [$i];
					
					$field->field_width = $field->field_width ? $field->field_width : 150;
					$field->field_height = $field->field_height ? $field->field_height : 200;
					$r = JHTML::_ ( 'select.genericlist', $select_options, $field->field_name . '[]', "$mouse_event $class size='4' $javascript multiple style=\"width:{$field->field_width}px;height:{$field->field_height}px;\"", 'value', 'text', $value );
				} else {
					
//					JRequest::setVar ( 'field_id', $field->table_name . '_' . $field->field_name );
//					$arr_option = JobBoardConstant::getApplyJobOptions();
					$enable_option = $jbconfig["posts"]->get('apply_option');
					if ($enable_option)
						$enable_option = explode(',',$enable_option);
					else 
						$enable_option = array('1','2','3');
					$r = '';
					for($i = count ( $arr_option )-1; $i >=0; $i--) 
						if (in_array($arr_option[$i]->value,$enable_option))
						{
							$selected = '';
							$style = "none";	
							if (!is_array($value))					
								$value = array();					
							if (in_array ( $arr_option [$i]->value, $value )) {
								$selected = 'checked="checked"';
								$style = "";
							}
							
							
							if ($arr_option [$i]->value == 1) {
								
								$r .= '<input ' . $selected . ' onclick="select_direct_url(this.checked)" type="checkbox" name="apply_type[]" id="apply_type'.$arr_option [$i]->value.'" value="' . $arr_option [$i]->value . '" /><label for="apply_type'.$arr_option [$i]->value.'">' . JText::_($arr_option [$i]->text) . '</label>';							
								$r .= ' <span style="display:'.$style.';vertical-align:top;" id="s_direct_url" ><br />';
								$direct_url_field = getItemByFieldName('ja_form_fields','field_name',"'direct_url'");
								for ($ii=0;$ii<count($direct_url_field);$ii++)
									if ($direct_url_field[$ii]->table_name=='ja_jobs')
									{
										$direct_url_field = $direct_url_field[$ii];
										break;
									}
									
								$r .= rendfield($item,$direct_url_field,'',true);
								
								$r .= '</span>';
							}
							else{
								$r .= '<input ' . $selected . '  type="checkbox" name="apply_type[]" id="apply_type'.$arr_option [$i]->value.'" value="' . $arr_option [$i]->value . '" /><label for="apply_type'.$arr_option [$i]->value.'">' . JText::_($arr_option [$i]->text) . '</label>';
							}
							
							if ($i > 0)
								$r .= '<br />';
						}
				}
				break;
			
			case 'RADIO' :
				JRequest::setVar ( 'field_id', $field->table_name . '_' . $field->field_name );
				$arr_option = $model_codes->getItems ();
				
				$field->field_height = $field->field_height ? $field->field_height : 18;
				$field->field_width = $field->field_width ? $field->field_width : 150;
				
				$r = JHTML::_ ( 'select.radiolist', $arr_option, $field->field_name . '[]', " $mouse_event $class $javascript size=\"1\" style=\"width:{$field->field_width}px;height:{$field->field_height}px;\"", 'value', 'text', $value );
				break;
			case 'SEPERATOR' :
				$r = "$value";
				break;
			case 'SELECT' :
				JRequest::setVar ( 'field_id', $field->table_name . '_' . $field->field_name );
				$arr_option = $model_codes->getItems ();
				
				$field->field_height = $field->field_height ? $field->field_height : 18;
				$field->field_width = $field->field_width ? $field->field_width : 150;
				$select_options = array ();
				$select_options [0]->value = '';
				if ($is_filter)
					$select_options [0]->text = JText::_ ( 'Please select' ).' '.$field->label . '...';
				else 
					$select_options [0]->text = JText::_ ( 'Please select' ). '...';
				for($i = 0; $i < count ( $arr_option ); $i ++)
					$select_options [] = $arr_option [$i];
					
				$r = JHTML::_ ( 'select.genericlist', $select_options, $field->field_name, " $mouse_event $class size=\"1\" $javascript style=\"width:{$field->field_width}px;height:{$field->field_height}px;\"", 'value', 'text', $value );
				break;
			
			case 'CHECKBOXLIST' :
				JRequest::setVar ( 'field_id', $field->table_name . '_' . $field->field_name );
				$arr_option = $model_codes->getItems ();
				if ($arr_option) {
					foreach ( $arr_option as $k => $op ) {
						$check = '';
						settype ( $value, 'array' );
						if (in_array ( $op->value, $value ))
							$check = 'checked';
						$r .= "<input $mouse_event $class type='checkbox'  name='$field->field_name[]' id=' " . $field->field_name . $k . "' value='$op->value' $check /><label for='$field->field_name.$k '>" . JText::_ ( $op->text ) . "</label><br/>";
					}
				}
				break;
			case 'TEXTAREA' :
				$field->field_height = $field->field_height ? $field->field_height : 5;
				$field->field_width = $field->field_width ? $field->field_width : 60;
				$r = "<textarea $mouse_event  $class cols='$field->field_width' rows='$field->field_height' name='$field->field_name' id='$field->field_name'>$value</textarea>";
				break;
		}
	}
	
	$r .= JText::_ ( $comment );
	if (($field->is_required == '1') && (! $field->is_readonly) && ! $javascript)
		$r .= '<span style="color:red;">*</span>';
	if ($example != '')
		$r .= '<br/>' . JText::_ ( $example );
	
	return $r;
}

/**
 * @desc 
 */
function getExternalCombo($field, $selected = "", $is_readonly = 0, $javascript = '', $add_empty = 1, $class = '', $mouse_event = '',$is_filter=false) {
	$arr_admin = array ('Manager', 'Administrator', 'Super Administrator' );
	$user = JFactory::getUser ();
	$db = &JFactory::getDBO ();
	$key = $field->referred_key;
	$value = $field->referred_value;
	$table = $field->referred_table;
	$r = '';
	if($selected == "") $selected = "0";
	$query = "SELECT * FROM #__$table ";
	
	if ($is_readonly) {
		$query .= " WHERE `$key`=$selected";
		$query .= " ORDER BY $value ";
		$db->setQuery ( $query, 0, 1 );
		
		$object = $db->loadObject ();		
		
		$r = isset ( $object->$value ) ? $object->$value : JText::_ ( 'NULL' );
	} else {
		if ($field->field_type!='SELECT'&&$field->field_type!='MSELECT')
		{
			$query .= " WHERE `$key`=$selected";
		}
		$db->setQuery ( $query );
		$objects = $db->loadObjectList ();
		
		$objectHTML = array ();
		if ($add_empty)
		{
			if ($is_filter)
				$objectHTML [] = JHTML::_ ( 'select.option', "", JText::_ ( "Please select" ).' '.$field->label . '...' );
			else 
				$objectHTML [] = JHTML::_ ( 'select.option', "", JText::_ ( "Please select" ) . '...' );
		}
				if ($field->referred_table == 'ja_categories' || $field->referred_table == 'ja_location') {
			$children = array ();
			if ($objects) {
				foreach ( $objects as $v ) {
					$pt = $v->parent_id;
					$list = @$children [$pt] ? $children [$pt] : array ();
					array_push ( $list, $v );
					$children [$pt] = $list;
				}
				if (is_array ( $children )) {
					
					foreach ( $children as $key => $value ) {
						$temp = array ();
						$tmp_children = array ();
						if (is_array ( $children [$key] )) {
							foreach ( $children [$key] as $k => $v )
								$temp [$k] = $v->name;
							asort ( $temp );
							foreach ( $temp as $k => $v )
								$tmp_children [$k] = $children [$key] [$k];
							$children [$key] = $tmp_children;
						}
					}
				
				}
				$list = treerecurse ( 0, '', array (), $children );
				if ($list) {
					foreach ( $list as $i => $item ) {
						$treename = $item->treename;
						$treename = JFilterOutput::ampReplace ( $treename );
						$treename = str_replace ( '"', '&quot;', $treename );
						if ($item->id == 0)
							$list [$i]->treename = $treename;
						else
							$list [$i]->treename = $treename;
					}
				}
				$objects = $list;
			}
			if (is_array($objects))
			foreach ( $objects as $key => $value ) {
				$objectHTML [] = JHTML::_ ( 'select.option', $key, $value->treename );
			}
		} else {
			for($i = 0, $n = count ( $objects ); $i < $n; $i ++) {
				$objectHTML [] = JHTML::_ ( 'select.option', $objects [$i]->$key, $objects [$i]->$value );
			}
		}
		
		if (! $field->field_width)
			$field->field_width = 150;
		
		if ($javascript != '')
			$type = 'SELECT';
		else
			$type = $field->field_type;
		switch ($type) {
			case 'MSELECT' :
				$field->field_width = $field->field_width ? $field->field_width : 150;
				$field->field_height = $field->field_height ? $field->field_height : 200;
				$r = JHTML::_ ( 'select.genericlist', $objectHTML, $field->field_name . '[]', "  $mouse_event $class size='4' $javascript multiple style=\"width:{$field->field_width}px;height:{$field->field_height}px;\"", 'value', 'text', $selected );
				if (JRequest::getVar('view')=='jajobs')
					$r = str_replace('<option','<option onclick="child_selection(document.getElementById(\''.$field->field_name.'\'),this)"',$r);
				break;
			case 'SELECT' :
				
				$r = JHTML::_ ( 'select.genericlist', $objectHTML, $field->field_name, " $mouse_event $class $javascript style=\"width:{$field->field_width}px;\"", 'value', 'text', $selected );
				break;
			default :
				$field->field_height = $field->field_height ? $field->field_height : 18;
				$field->field_width = $field->field_width ? $field->field_width : 150;
				$r = "<input $mouse_event $class type='text' name='" . $field->field_name . "' id='" . $field->field_name . "' value='" . $selected . "' style=\"width:" . $field->field_width . "px;height:" . $field->field_height . "px;\" /> ";
		
		}
	}
	
	return $r;
}

function treerecurse($id, $indent, $list, &$children, $maxlevel = 9999, $level = 0, $type = 1) {
	if (@$children [$id] && $level <= $maxlevel) {
		if (is_array ( $children ))
			foreach ( $children [$id] as $v ) {
				$id = $v->id;
				
				if ($type) {
					$pre = '<sup>|_</sup>&nbsp;';
					$spacer = '.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
				} else {
					$pre = '- ';
					$spacer = '&nbsp;&nbsp;';
				}
				
				if ($v->parent_id == 0) {
					$txt = $v->name;
				} else {
					$txt = $pre . $v->name;
				}
				$pt = $v->parent_id;
				$list [$id] = $v;
				$list [$id]->treename = "$indent$txt";
				$list [$id]->children = count ( @$children [$id] );
				$list = treeRecurse ( $id, $indent . $spacer, $list, $children, $maxlevel, $level + 1, $type );
			}
	}
	
	return $list;
}

function rendfield_JS() {
	?>
<script language="javascript" type="text/javascript">
	/* <![CDATA[ */
		urlroot = '<?php
	echo JURI::base ();
	?>';
		function remove_attach(field_name,item_id)
		{
			if (!confirm('<?php
	echo JText::_ ( 'Do you want to remove attach file ?' );
	?>'))
				return;
			var url = "index.php?option=com_jajobboard&view=<?php
	echo JRequest::getVar ( 'view' );
	?>&task=remove_attach&field_name="+field_name+"&item_id="+item_id;
			new Ajax(url, {method:'get', onComplete:updateRemoveAttach(field_name)}).request();
		}
		function updateRemoveAttach(text)
		{
			view = '<?php
	echo JRequest::getVar ( 'view' );
	?>';
			if (text!='')
			{
				document.getElementById('download_'+text).innerHTML = '';
				document.getElementById('text_'+text).value = '';
				document.getElementById('remove_'+text).innerHTML = '';
				if (view=='jaapplications')
				{
					document.getElementById('opt_attachment0').checked = true;
					document.getElementById(text).disabled = false;
				}
			}
		}
		
		function remove_image(field_name,item_id)
		{
			if (!confirm('<?php
	echo JText::_ ( 'Do you want to remove image ?' );
	?>'))
				return;
			var url = "index.php?option=com_jajobboard&view=<?php
	echo JRequest::getVar ( 'view' );
	?>&task=remove_attach&field_name="+field_name+"&item_id="+item_id;
			new Ajax(url, {method:'get', onComplete:updateRemoveImage(field_name)}).request();
		}
		function updateRemoveImage(text)
		{
			view = '<?php
	echo JRequest::getVar ( 'view' );
	?>';
			if (text!='')
			{
				document.getElementById('download_'+text).innerHTML = "<img src='<?php
	echo JURI::base ();
	?>/components/com_jajobboard/images/no-image.gif' alt='Default'/>";
				document.getElementById('text_'+text).value = '';
				document.getElementById('remove_'+text).innerHTML = '';
				if (view=='jaapplications')
				{
					document.getElementById('opt_attachment0').checked = true;
					document.getElementById(text).disabled = false;
				}
			}
		}
	/* ]]> */
	</script>
<?php
}

// To standard time
function jbToStandardTime($str){
	$str = date( "Y-m-d H:i:s", strtotime($str));
}

function jbToCustomeTime($str){
	$str = date( "Y-m-d H:i:s", strtotime($str));
}
?>